<script src="../../../api/system/dict/data.js"></script>
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width=" ">
      <!--      <el-form-item label="头像地址" prop="avatarAddress">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.avatarAddress"-->
      <!--          placeholder="请输入头像地址"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入昵称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="realName">
        <el-input v-model="queryParams.realName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="微信号" prop="wechat">
        <el-input v-model="queryParams.wechat" placeholder="请输入微信号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="头衔id" prop="titleCode"> -->
      <!--        <el-input-->
      <!--          v-model="queryParams.titleCode"-->
      <!--          placeholder="请输入头衔id"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!-- </el-form-item> -->
      <el-form-item label="账号" prop="account">
        <el-input v-model="queryParams.account" placeholder="请输入账号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--      <el-form-item label="密码" prop="password">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.password"-->
      <!--          placeholder="请输入密码"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!-- <el-form-item label="个人简介" prop="personResume"> -->
      <!--        <el-input-->
      <!--          v-model="queryParams.personResume"-->
      <!--          placeholder="请输入个人简介"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="所属组织" prop="organization">
        <el-input v-model="queryParams.organization" placeholder="请输入所属组织" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="删除标识" prop="isDel">
        <template slot="label">
          <span>
            删除标识
            <el-tooltip class="item" effect="dark" content="0-正常，1-停用" placement="top">
              <i class="el-icon-question"></i>
            </el-tooltip>
          </span>
        </template>
        <el-input v-model="queryParams.isDel" clearable placeholder="请输入删除标识" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--      <el-form-item label="删除标识"  prop="isDel">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.isDel"-->
      <!--          placeholder="请输入删除标识（0正常，1停用）"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="扩展字段1" prop="ext1">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.ext1"-->
      <!--          placeholder="请输入扩展字段1"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="扩展字段2" prop="ext2">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.ext2"-->
      <!--          placeholder="请输入扩展字段2"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="扩展字段3" prop="ext3">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.ext3"-->
      <!--          placeholder="请输入扩展字段3"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="扩展字段4" prop="ext4">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.ext4"-->
      <!--          placeholder="请输入扩展字段4"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="扩展字段5" prop="ext5">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.ext5"-->
      <!--          placeholder="请输入扩展字段5"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
    </el-form>

    <el-row :gutter="10" class="mb8" style="display: flex;">
      <el-col style="justify-content: left;" :span="20">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:userInfo:add']">新增</el-button>
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:userInfo:edit']">修改</el-button>
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:userInfo:remove']">删除</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:userInfo:export']">导出</el-button>
      </el-col>
      <el-col style="justify-content: right;" class="text-right" v-show="showSearch" :span="2.5">
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :span="1.5"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="手机号" align="center" width="120" prop="phone" />
      <el-table-column label="姓名" align="center" prop="realName" />
      <!-- <el-table-column label="性别" align="center" prop="sex" /> -->
      <!-- <el-table-column label="用户头像" align="center" prop="avatarAddress">
        <template slot-scope="scope">
          <p v-if="scope.row.avatarAddress == null">无图片</p>
          <image-preview v-if="scope.row.avatarAddress != null" :src="scope.row.avatarAddress" :width="50"
            :height="50" />
        </template>
</el-table-column> -->
      <el-table-column label="微信号" align="center" width="200" prop="wechat" />
      <!-- <el-table-column label="头衔id" align="center" prop="titleCode" /> -->
      <el-table-column label="账号" align="center" width="150" prop="account" />
      <el-table-column label="openid" align="center" width="150" prop="openid" />
      <el-table-column label="sessionKey" align="center" width="150" prop="sessionKey" />
      <!-- <el-table-column label="密码" align="center" width="200" prop="password" /> -->
      <!-- <el-table-column label="个人简介" align="center" width="150" prop="personResume" /> -->
      <!-- <el-table-column label="所属组织" align="center" width="150" prop="organization" /> -->
      <el-table-column label="累计减碳量" align="center" prop="co2Reduce" />
      <el-table-column label="积分数量" align="center" prop="points" />
      <el-table-column label="累计减碳天数" align="center" prop="reductionTime" />
      <el-table-column label="删除标识" align="center" prop="isDel" />
      <!--      <el-table-column label="扩展字段3" align="center" prop="ext3" />-->
      <!--      <el-table-column label="扩展字段4" align="center" prop="ext4" />-->
      <!--      <el-table-column label="扩展字段5" align="center" prop="ext5" />-->
      <!-- <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="更新时间" align="center" prop="updateTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['monitor:job:query']">详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改用户信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="openEdit" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="用户id自动生成，不必填写" readonly />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-model="form.sex" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="头像" prop="avatarAddress">
          <image-avatar-upload v-model="form.avatarAddress" />
        </el-form-item>
        <el-form-item label="微信号" prop="wechat">
          <el-input v-model="form.wechat" placeholder="请输入微信号" />
        </el-form-item>
        <el-form-item label="头衔id" prop="titleCode">
          <el-input v-model="form.titleCode" placeholder="请输入头衔id" />
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="个人简介" prop="personResume">
          <el-input v-model="form.personResume" placeholder="请输入个人简介" />
        </el-form-item>
        <el-form-item label="所属组织" prop="organization">
          <el-input v-model="form.organization" placeholder="请输入所属组织" />
        </el-form-item>
        <el-form-item label="openid" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入openid" />
        </el-form-item>
        <el-form-item label="sessionKey" prop="sessionKey">
          <el-input v-model="form.sessionKey" placeholder="请输入sessionKey" />
        </el-form-item>
        <el-form-item label="累计减碳量" prop="co2Reduce">
          <el-input v-model="form.co2Reduce" placeholder="请输入累计减碳量" />
        </el-form-item>
        <el-form-item label="积分数量" prop="points">
          <el-input v-model="form.points" placeholder="请输入积分数量" />
        </el-form-item>
        <el-form-item label="累计减碳天数" prop="reductionTime">
          <el-input v-model="form.reductionTime" placeholder="请输入累计减碳天数" />
        </el-form-item>
        <el-form-item label="删除标识" prop="isDel">
          <template slot="label">
            <span>
              删除标识
              <el-tooltip class="item" effect="dark" content="0-正常，1-停用" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </template>
          <el-input v-model="form.isDel" placeholder="请输入删除标识（0-正常，1-停用）" />
        </el-form-item>
        <!--        <el-form-item label="扩展字段3" prop="ext3">-->
        <!--          <el-input v-model="form.ext3" placeholder="请输入扩展字段3" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="扩展字段4" prop="ext4">-->
        <!--          <el-input v-model="form.ext4" placeholder="请输入扩展字段4" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="扩展字段5" prop="ext5">-->
        <!--          <el-input v-model="form.ext5" placeholder="请输入扩展字段5" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看用户信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="openView" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="用户id自动生成，不必填写" readonly />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="未填入昵称" readonly />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="未填入手机号" readonly />
        </el-form-item>
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="未填入姓名" readonly />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-model="form.sex" placeholder="未填入性别" readonly />
        </el-form-item>
        <el-form-item label="头像" prop="avatarAddress">
          <template slot-scope="scope">
            <p v-if="form.avatarAddress == null">无图片</p>
            <image-preview v-if="form.avatarAddress != null" :src="form.avatarAddress" :width="50"
              :height="50" />
          </template>
        </el-form-item>
        <el-form-item label="微信号" prop="wechat">
          <el-input v-model="form.wechat" placeholder="未填入微信号" readonly />
        </el-form-item>
        <el-form-item label="头衔id" prop="titleCode">
          <el-input v-model="form.titleCode" placeholder="未填入头衔id" readonly />
        </el-form-item>
        <el-form-item label="头衔名" prop="titleName">
          <el-input v-model="form.titleName" placeholder="未填入头衔名" readonly @click.native="handleClickTitleDict" />
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input v-model="form.account" placeholder="未填入账号" readonly />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="未填入密码" readonly />
        </el-form-item>
        <el-form-item label="个人简介" prop="personResume">
          <el-input v-model="form.personResume" placeholder="未填入个人简介" readonly />
        </el-form-item>
        <el-form-item label="所属组织" prop="organization">
          <el-input v-model="form.organization" placeholder="未填入所属组织" readonly />
        </el-form-item>
        <el-form-item label="openid" prop="openid">
          <el-input v-model="form.openid" placeholder="未填入openid" readonly />
        </el-form-item>
        <el-form-item label="sessionKey" prop="sessionKey">
          <el-input v-model="form.sessionKey" placeholder="未填入sessionKey" readonly />
        </el-form-item>
        <el-form-item label="累计减碳量" prop="co2Reduce">
          <el-input v-model="form.co2Reduce" placeholder="未填入累计减碳量" readonly />
        </el-form-item>
        <el-form-item label="积分数量" prop="points">
          <el-input v-model="form.points" placeholder="未填入积分数量" readonly />
        </el-form-item>
        <el-form-item label="累计减碳天数" prop="reductionTime">
          <el-input v-model="form.reductionTime" placeholder="未填入累计减碳天数" readonly />
        </el-form-item>
        <el-form-item label="删除标识" prop="isDel">
          <template slot="label">
            <span>
              删除标识
              <el-tooltip class="item" effect="dark" content="0-正常，1-停用" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </template>
          <el-input v-model="form.isDel" placeholder="未填入删除标识（0-正常，1-停用）" readonly />
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listUserInfo, getUserInfo, delUserInfo, addUserInfo, updateUserInfo } from "@/api/system/userInfo";
import { getDicts } from "@/api/system/dict/data"
import userInfo from "../user/profile/userInfo";

export default {
  name: "UserInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户信息管理表格数据
      userInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      openEdit: false,
      openView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        avatarAddress: null,
        nickName: null,
        phone: null,
        realName: null,
        sex: null,
        wechat: null,
        titleCode: null,
        account: null,
        password: null,
        personResume: null,
        organization: null,
        openid: null,
        sessionKey: null,
        isDel: null,
        reductionTime: null,
        co2Reduce: null,
        points: null,
        // ext3: null,
        // ext4: null,
        // ext5: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户信息管理列表 */
    async getList() {
      this.loading = true;
      const response = await listUserInfo(this.queryParams);
      this.userInfoList = response.rows;
      this.total = response.total;
      await this.updateTitleName();
      this.loading = false;
    },
    // 获取用户头衔的值
    async updateTitleName() {
      let response = await getDicts("user_title");
      let titleList = response.data;
      console.log(titleList);
      console.log(this.userInfoList);
      for (let i = 0; i < this.userInfoList.length; i++) {
        if (this.userInfoList[i].titleCode!= null) {
          let titleName = titleList.find(item => item.dictSort === this.userInfoList[i].titleCode).dictLabel;
          this.$set(this.userInfoList[i], "titleName", titleName);
        }
      }
    },
    handleClickTitleDict() {
      // 这里可以弹出确认框，或者直接跳转
      this.$confirm('确定将前往“头衔字典管理”修改?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        // 用户点击确认后的逻辑，例如跳转
        this.goToTitleDict();
      }).catch(() => {
        // 用户点击取消后的逻辑（如果有需要）
      });
    },
    // 跳转至头衔字典页面
    goToTitleDict() {
      console.log("跳转至头衔字典管理页面");
      this.$router.push({ path: '/system/dict-data/index/11' });
      this.openView = false;
    },
    // 取消按钮
    cancel() {
      this.openEdit = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        avatarAddress: null,
        nickName: null,
        phone: null,
        realName: null,
        sex: null,
        wechat: null,
        titleCode: null,
        account: null,
        password: null,
        personResume: null,
        organization: null,
        openid: null,
        sessionKey: null,
        isDel: null,
        createTime: null,
        updateTime: null,
        reductionTime: null,
        co2Reduce: null,
        points: null,
        // ext3: null,
        // ext4: null,
        // ext5: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.openView = true;
      this.title = "查看用户信息管理（如需修改，请通过修改按钮进行修改）";
      this.form = row;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openEdit = true;
      this.title = "添加用户信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getUserInfo(userId).then(response => {
        this.form = response.data;
        this.openEdit = true;
        this.title = "修改用户信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateUserInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openEdit = false;
              this.getList();
            });
          } else {
            addUserInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openEdit = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除用户信息管理Id为"' + userIds + '"的数据项？').then(function () {
        return delUserInfo(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/userInfo/export', {
        ...this.queryParams
      }, `userInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
